package com.twt.gym.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.twt.common.consts.SysDicts;
import com.twt.common.exceptions.AppException;
import com.twt.common.pojo.Result;
import com.twt.common.utils.Assert;
import com.twt.gym.service.TGymCoachService;
import com.twt.model.member.entity.TGymCoach;
import com.twt.model.member.vo.TGymCoachQueryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * (t_gym_coach)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/coach")
public class TGymCoachController {
    @Autowired
    private TGymCoachService coachService;

    @GetMapping("/query/page/{currentPage}/{pageSize}")
    public Result queryPageGymMember(@PathVariable("currentPage") long currentPage, @PathVariable("pageSize") long pageSize, TGymCoachQueryVo queryVo) {
        Page<TGymCoach> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageSize);

        QueryWrapper<TGymCoach> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(queryVo.getName())) {
            queryWrapper.like("name", queryVo);
        }
        Page<TGymCoach> memberPage = coachService.page(page, queryWrapper);
        return Result.ok(memberPage);
    }

    @GetMapping("/query/one/{id}")
    public Result queryPageGymMember(@PathVariable("id") String id) {
        Assert.isBlank(id, "id 不能为空！");
        QueryWrapper<TGymCoach> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("id", id);

        TGymCoach one = coachService.getOne(queryWrapper);
        return Result.ok(one);
    }

    @PostMapping("/add")
    public Result add(TGymCoach coach) {
        this.beforeAddOrUpdateValidate(coach);

        QueryWrapper<TGymCoach> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", coach.getName());
        queryWrapper.eq("is_deleted", SysDicts.DELETE_NO);
        TGymCoach one = coachService.getOne(queryWrapper);
        if (one != null) {
            throw new AppException(String.format("课程名称【%s】已经存在，请重新输入"));
        }
        coachService.save(coach);
        return Result.ok();
    }

    @PutMapping("/update")
    public Result update(TGymCoach coach) {
        this.beforeAddOrUpdateValidate(coach);
        coachService.updateById(coach);
        return Result.ok();
    }

    @PutMapping("/updateImgUrl")
    public Result updateImgUrl(TGymCoach coach) {
        Assert.isBlank(coach.getId(), "id 不能为空！");
        Assert.isBlank(coach.getImageUrl(), "图片地址 不能为空！");
        coach.setImageUrl(String.format("fileserver/showImg/%s", coach.getImageUrl()));
        coachService.updateById(coach);
        return Result.ok();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") String id) {
        Assert.isBlank(id, "id 不能为空！");
        coachService.removeById(id);
        return Result.ok();
    }

    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(String ids) {
        Assert.isBlank(ids, "ids 不能为空！");
        Arrays.asList(ids.split(",")).forEach(id -> {
            coachService.removeById(id);
        });
        return Result.ok();
    }


    private void beforeAddOrUpdateValidate(TGymCoach coach) {
        Assert.isBlank(coach.getName(), "教练 不能为空！");
        Assert.isBlank(coach.getTel(), "电话号码 不能为空！");
        Assert.isBlank(coach.getRemark(), "教练简介 不能为空！");
    }
}
